package com.jxs.tourist.controller;

import com.jxs.tourist.domain.Toilet;
import com.jxs.tourist.service.ToiletService;
import com.jxs.tourist.util.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/toilet")
public class ToiletController {
    @Resource
    ToiletService toiletService;
    public ToiletController(){
        System.out.println("init");
    }
    /**
     * 添加
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value ="/addToilet")
    public JsonResult<String> addToilet(HttpServletRequest request){
        String cate_name=request.getParameter("cate_name");
        Toilet toilet=new Toilet();
        toilet.setCreate_time(new Date());
        toilet.setCate_name(cate_name);
        if(toiletService.addToilet(toilet)==1){
            return new JsonResult<String>("","添加成功",0);
        }
        return new JsonResult<String>("","添加失败",1);
    }
    /**
     * 编辑
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value ="/editToilet")
    public JsonResult<String> editToilet(HttpServletRequest request){
        int toilet_id=Integer.valueOf(request.getParameter("id"));
        Toilet toilet=toiletService.getToiletById(toilet_id);
        if(toilet==null){
            return new JsonResult<String>("","不存在",1);
        }

        String cate_name=request.getParameter("cate_name");
        toilet.setCate_name(cate_name);
        if(toiletService.editToilet(toilet)==1){
            return new JsonResult<String>("","编辑成功",0);
        }
        return new JsonResult<String>("","编辑失败",1);
    }
    /**
     * 删除
     * @param toilet_id
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteToilet")
    public JsonResult<String> deleteToilet(@RequestParam("id") int toilet_id, HttpServletRequest request){
        if(toiletService.delToiletById(toilet_id)==1){
            return new JsonResult<String>("","删除成功",0);
        }
        return new JsonResult<String>("","删除失败",1);
    }
    /**
     * 查询所有卫生间
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAllToilet",method = RequestMethod.POST)
    public JsonResult<List<Toilet>> getAllToilet(HttpServletRequest request) {
        List<Toilet> list = toiletService.getAllToilet();
        JsonResult<List<Toilet>> result;
        if (list == null || list.size() == 0) {
            result = new JsonResult<List<Toilet>>(list, "暂时没有数据", 1);
        } else {
            result = new JsonResult<List<Toilet>>(list, "获取成功", 0);
        }
        return result;
    }
    /**
     * 根据景点查找
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAllToiletByTouristId",method = RequestMethod.POST)
    public JsonResult<List<Toilet>> getAllToiletByTouristId(HttpServletRequest request) {
        int city_id=Integer.valueOf(request.getParameter("toilet_id"));
        List<Toilet> list = toiletService.getToiletByTouristId(city_id);
        JsonResult<List<Toilet>> result;
        if (list == null || list.size() == 0) {
            result = new JsonResult<List<Toilet>>(list, "暂时没有数据", 1);
        } else {
            result = new JsonResult<List<Toilet>>(list, "获取成功", 0);
        }
        return result;
    }
}
